package com.yanqu.road.server.cmd.union.cross;

import com.yanqu.road.entity.enums.eUnionNoticeType;
import com.yanqu.road.entity.enums.eUnionPosition;
import com.yanqu.road.entity.player.CrossUserBaseInfo;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionMgr;
import com.yanqu.road.server.manager.union.CrossUnionNoticeMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = Protocol.C_UNION_CHANGE_RANDOM_JOIN, desc = "修改随机加入")
public class CrossChangeRandomJoinCmd extends CrossCommand {


    long playerId;

    UnionProto.UnionChangeRandomJoinRespMsg.Builder respMsg = UnionProto.UnionChangeRandomJoinRespMsg.newBuilder();

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        playerId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        UnionProto.UnionChangeRandomJoinReqMsg reqMsg = UnionProto.UnionChangeRandomJoinReqMsg.parseFrom(packet.getBytes());
        boolean isAllowRandomJoin = reqMsg.getIsAllowRandomJoin();

        CrossUserBaseInfo crossUserBaseInfo = CrossUserMgr.getCrossUserBaseInfo(packet.getPlayerId());
        if(crossUserBaseInfo == null){
            ret = GameErrorCode.E_USER_NO_FOUND;
            return;
        }

        CrossUnionMember member = CrossUnionMgr.getUnionMember(crossUserBaseInfo.getUserBaseInfo().getUnionUid(), playerId);
        if(member == null || StringUtils.isNullOrEmpty(member.getUnionUid())){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }

        CrossUnionInfo unionInfo = CrossUnionMgr.getUnionInfo(member.getUnionUid());
        if(unionInfo == null){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }

        if (member.getPosition() == eUnionPosition.Master.getValue()
                || member.getPosition() == eUnionPosition.DeputyMaster.getValue()) {

            unionInfo.setAllowRandomJoin(isAllowRandomJoin);
            //增加联盟日志
            CrossUnionNoticeMgr.addNotice(crossUserBaseInfo, unionInfo.getUnionUid(), eUnionNoticeType.UnionModify.getValue(), "");
            //RankMgr.changeUnionRank(unionInfo);
        } else {
            ret = GameErrorCode.E_UNION_POSITION_NO_ENOUGH;
        }

    }

    @Override
    public void sendData() {

        respMsg.setRet(ret);
        MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_UNION_CHANGE_RANDOM_JOIN, respMsg));
    }
}
